import { Doorplate, UploadResourceChunk, UploadResourceInformation } from "@/model/ResourceModels"
import axios from "axios"
import { Ref } from "vue"


// 初始化仓库
export const initializeTheRepositoryAPI = async (userId: number):
    Promise<string | null> => {
    console.log(`API初始化仓库用户id为 ${userId}...`)
    try {
        // 发送初始化仓库请求
        const response = await axios.post('/api/upload/initializeTheRepository',
            userId,
            { headers: { 'Content-Type': 'application/json' } }
        )
        console.log("后端返回的数据：", response.data)              //调试
        return response.data
    } catch (error) {
        // 初始化仓库失败，捕获异常
        console.error('获取资源数据失败:', error)
        return null
    }
}


export const heartBeatsAPI = async (doorplate: Doorplate): Promise<boolean> => {
    console.log(`API心跳 ${doorplate.userId}:${doorplate.secondaryPath} 跳动...`)
    try {
        // 发送心脏跳动
        const response = await axios.post('/api/upload/heartBeats',
            doorplate,
            { headers: { 'Content-Type': 'application/json' } }
        )
        console.log("后端返回的数据：", response.data)              //调试
        return response.data
    } catch (error) {
        // 心脏跳动失败，捕获异常
        console.error('获取资源数据失败:', error)
        return false
    }
}

export const uploadChunkAPI = async (uploadResourceChunk: UploadResourceChunk): Promise<boolean> => {
    console.log(`API上传分片 ${uploadResourceChunk.chunkIndex}...`)
    try {
        // 发送上传分片请求
        const response = await axios.post('/api/upload/uploadChunk',
            uploadResourceChunk,
            { headers: { 'Content-Type': 'application/json' } }
        )
        console.log("后端返回的数据：", response.data)              //调试
        return response.data
    } catch (error) {
        // 上传分片失败，捕获异常
        console.error('获取资源数据失败:', error)
        return false
    }
}

export const SplicingChunksAPI = async (uploadResourceInformation: UploadResourceInformation): Promise<boolean> => {
    console.log(`API上传结束 ${uploadResourceInformation}...`)
    try {
        // 发送结束请求
        const response = await axios.post('/api/upload/splicingChunks',
            uploadResourceInformation,
            { headers: { 'Content-Type': 'application/json' } }
        )
        console.log("后端返回的数据：", response.data)              //调试
        return response.data
    } catch (error) {
        // 发送结束请求失败，捕获异常
        console.error('获取资源数据失败:', error)
        return false
    }
}

export const cancelUploadAPI = async (doorplate: Doorplate): Promise<boolean> => {
    console.log(`API取消上传 ${doorplate.userId}:${doorplate.secondaryPath}...`)
    try {
        // 发送取消上传请求
        const response = await axios.post('/api/upload/cancelUpload',
            doorplate,
            { headers: { 'Content-Type': 'application/json' } }
        )
        console.log("后端返回的数据：", response.data)              //调试
        return response.data
    } catch (error) {
        // 取消上传失败，捕获异常
        console.error('获取资源数据失败:', error)
        return false
    }
}


